package cn.jiangjiesheng.bootstrap.commons.utils;

/**
 * Created by dev@jiangjiesheng.cn on 2020/7/27.
 */
public class StringUtils {

    /**
     * 处理url参数中的中文
     * @param str
     * @return
     */
    public static String encodeURI(String str) {
        try {
            String isoStr = new String(str.getBytes("UTF8"), "ISO-8859-1");
            char[] chars = isoStr.toCharArray();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < chars.length; i++) {
                if ((chars[i] <= 'z' && chars[i] >= 'a') || (chars[i] <= 'Z' && chars[i] >= 'A')
                        || chars[i] == '-' || chars[i] == '_' || chars[i] == '.' || chars[i] == '!'
                        || chars[i] == '~' || chars[i] == '*' || chars[i] == '\'' || chars[i] == '('
                        || chars[i] == ')' || chars[i] == ';' || chars[i] == '/' || chars[i] == '?'
                        || chars[i] == ':' || chars[i] == '@' || chars[i] == '&' || chars[i] == '='
                        || chars[i] == '+' || chars[i] == '$' || chars[i] == ',' || chars[i] == '#'
                        || (chars[i] <= '9' && chars[i] >= '0')) {
                    sb.append(chars[i]);
                } else {
                    sb.append("%");
                    sb.append(Integer.toHexString(chars[i]));
                }
            }
            return sb.toString();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }


}
